// 最优解，双指针方法
var removeDuplicates = function (nums) {
  if (nums.length === 0) return 0;

  let slow = 0;
  for (let fast = 1; fast < nums.length; fast++) {
    if (nums[fast] !== nums[slow]) {
      slow++;
      nums[slow] = nums[fast];
    }
  }

  return slow + 1;
};

// 从前面开始记录
var removeDuplicates = function (nums) {
  let count = 0;
  let arr = [];
  let len = nums.length;
  if (len === 1) {
    return 1;
  } else if (len === 0) {
    return 0;
  }

  for (let i = 1; i < len; i++) {
    if (nums[i] === nums[i - 1]) {
      arr.push(i);
    } else {
      count++;
    }
  }

  for (let i = arr.length - 1; i >= 0; i--) {
    nums.splice(arr[i], 1);
  }

  return count;
};

// 从后面开始删除
var removeDuplicates1 = function (nums) {
  let count = 0;
  let len = nums.length;
  if (len === 1) {
    return 1;
  } else if (len === 0) {
    return 0;
  }

  for (let i = len - 2; i >= 0; i--) {
    if (nums[i] === nums[i + 1]) {
      nums.splice(i, 1);
    } else {
      count++;
    }
  }

  return nums.length;
};

// 使用Set
// 这个写法有点问题，原数组也需要进行修改，但是这里没有进行修改
var removeDuplicates2 = function (nums) {
  const mySet = new Set();
  const len = nums.length;
  for (let i = 0; i < len; i++) {
    mySet.add(nums[i]);
  }

  return mySet.size;
};

let nums = [1, 1, 2];

console.log(removeDuplicates2(nums));
